package codeTop;

public class q221 {
    //暴力
    public int maximalSquare(char[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int max = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == '1') {
                    max = Math.max(func(matrix, i, j),max);
                }
            }
        }
        return max*max;
    }
    public int func(char[][] matrix, int i, int j){
        int m = matrix.length;
        int n = matrix[0].length;
        int maxWidth = Math.min(m - i, n - j);
        for (int width = 1; width <= maxWidth; width++) {
            for (int k = 0; k < width; k++) {
                if (matrix[i + width - 1][j + k] != '1') {
                    return width-1;
                }
                if (matrix[i + k][j + width - 1] != '1') {
                    return width-1;
                }
            }
        }
        return maxWidth;
    }
    //dp
    public int maximalSquare1(char[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] dp = new int[m+1][n+1];
        int max = 0;
        for (int i = 1; i <=m ; i++) {
            for (int j = 1; j <=n; j++) {
                if (matrix[i - 1][j - 1] == '1') {
                    dp[i][j] = Math.min(dp[i-1][j],Math.min(dp[i-1][j-1],dp[i][j-1]))+1;
                    max = Math.max(dp[i][j],max);
                }
            }
        }
        return max*max;
    }

    public static void main(String[] args) {
        q221 q = new q221();
        System.out.println(q.maximalSquare(new char[][]{{'0', '1'}, {'1', '0'}}));
    }
}
